package com.ruoyi.web.controller.stock;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.client.domain.SysHoldStock;
import com.ruoyi.client.domain.TbStockVo;
import com.ruoyi.client.service.ISysHoldStockService;
import com.ruoyi.client.service.ITbStockVoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.web.controller.test.CarMove;
import com.ruoyi.web.controller.test.Movable;
import com.ruoyi.web.controller.test.MoveLogHander;
import com.ruoyi.web.controller.test.PlanMove;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Proxy;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.utils.PageUtils.startPage;

@RestController
@RequestMapping("/stock/stockList")
public class StockController extends BaseController {

    @Autowired
    ISysHoldStockService iSysHoldStockService;
    @Autowired
    ITbStockVoService iTbStockVoService;
    @GetMapping("/list")
    public TableDataInfo list(TbStockVo stockData){
        startPage();
        List<TbStockVo> list= iTbStockVoService.selectTbStockVo(stockData);
        for (TbStockVo tbStockVo : list) {
            System.err.println(list);
        }

        return getDataTable(list);
    }

    /**
     * 实时更新股票列表数据
     * 对大量数据的一个处理
     * @return
     */
    @PreAuthorize("@ss.hasPermi('stock:stockList:synchronousUpdate')")
    @PostMapping("/synchronousUpdate")
    public AjaxResult synchronousUpdate(){
        //删除tb_stock_vo表中的所有数据
        iTbStockVoService.deleteAll();
        String url="http://web.juhe.cn/finance/stock/szall";
        String key="86a80b14bee6e5169a0d22826abc3333";
        HashMap<String, Object> map = new HashMap<>();
        map.put("key",key);
        final String jsonString = HttpUtil.get(url, map);
        //System.out.println(s);
        JSONObject jsonObject = new JSONObject(jsonString);
        JSONObject result = jsonObject.getJSONObject("result");
        //总页数
        final int num = Integer.parseInt((String) result.get("num"));
        //当前页
        String dbpage = (String) result.get("page");
        final int page = Integer.parseInt(dbpage);
        ArrayList<TbStockVo> tbStockVosAll = new ArrayList<>();
        for (int i = page; i < num; i++) {
            map.put("page",i);
            String jsonString1 = HttpUtil.get(url, map);
            JSONObject jsonObject1 = new JSONObject(jsonString1);
            JSONObject result1 = jsonObject1.getJSONObject("result");
            JSONArray data = result1.getJSONArray("data");
            System.err.println("data======="+data);
            List<TbStockVo> tbStockVos = tbStockVoList(data);
            for (TbStockVo tbStockVo : tbStockVos) {
                tbStockVosAll.add(tbStockVo);
            }
        }
        return success(tbStockVosAll);
    }
    
    public List<TbStockVo> tbStockVoList(JSONArray data){
        System.err.println(data);
        ArrayList<JSONObject> jsonObjects = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            jsonObjects.add(data.getJSONObject(i));
        }
        ArrayList<TbStockVo> tbStockVos = new ArrayList<>();
        for (JSONObject jsonData : jsonObjects) {
            TbStockVo stockData = new TbStockVo();
            stockData.setSymbol(jsonData.getStr("symol"));
            stockData.setName(jsonData.getStr("name"));
            stockData.setTrade(jsonData.getStr("trade"));
            stockData.setPriceChange(jsonData.getStr("pricechange"));
            stockData.setChangePercent(jsonData.getStr("changepercent"));
            stockData.setBuy(jsonData.getStr("buy"));
            stockData.setSell(jsonData.getStr("sell"));
            stockData.setSettlement(jsonData.getStr("settlement"));
            stockData.setOpen(jsonData.getStr("open"));
            stockData.setHigh(jsonData.getStr("high"));
            stockData.setLow(jsonData.getStr("low"));
            stockData.setVolume(jsonData.getInt("volume"));
            stockData.setAmount(jsonData.getLong("amount"));
            stockData.setCode(jsonData.getStr("code"));
            stockData.setTickTime(jsonData.getStr("ticktime"));
            tbStockVos.add(stockData);
            iTbStockVoService.add(stockData);
        }
        return tbStockVos;
    }


    //我的持仓
    @PostMapping("myPosition")
    public AjaxResult myPosition(){
        List<SysHoldStock> list=iSysHoldStockService.listAll();
        final Movable o1 = (Movable) Proxy.newProxyInstance(MoveLogHander.class.getClassLoader(), new Class[]{Movable.class}, new MoveLogHander(new PlanMove()));
        o1.move();
        return success(list);
    }


}
